<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once 'eleontev/SQL/SearchParams.php';


class KBGlossaryView_list extends AppView
{
	
	var $tmpl = 'list.html';
	
	
	function execute(&$obj, &$manager) {

		$tpl = &new tplTemplatez($this->template_dir . $this->tmpl);
		
		$search = &$this->searchForm();
		$manager->setSqlParams('AND '. $search['sql']);
		
		//letter filter
		$sql = $this->parseLetterFilter($tpl, $_GET);
		$manager->setSqlParams('AND ' . $sql);
		

		// header generate
		$bp =& $this->pageByPage($manager->limit, $manager->getRecordsSql());
		$tpl->tplAssign('header', $this->commonHeaderList($bp->nav, $search['form']));
		
		// sort generate
		$sort = &$this->getSort();
		$manager->setSqlParamsOrder($sort->getSql());
		
		// get records
		$rows =& $this->stripVars($manager->getRecords($bp->limit, $bp->offset));
		
		// alt phrases - not done
		//foreach($rows as $v) { $ids[] = $v['id']; }
		//$alt_phrases = &$manager->getAltPhrases(implode(',', $ids));
		
		
		// list records
		foreach($rows as $row) {
			
			$obj->set($row);
			$obj->set('display_once', ($obj->get('display_once') ? '<li>' : '-'));
			
			$tpl->tplAssign($this->getViewListVars($obj->get('id')));
			$tpl->tplParse(array_merge($obj->get(), $this->msg), 'row');
		}
		
		$tpl->tplAssign($this->msg);
		$tpl->tplAssign($sort->toHtml());
	
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function &getSort() {
		//$sort = new TwoWaySort();
		$sort = new OneWaySort($_GET);
		$sort->default_order = 1;
		$sort->setTitleMsg('asc',  $this->msg['sort_asc_msg']);
		$sort->setTitleMsg('desc', $this->msg['sort_desc_msg']);
		$sort->setSortItem('phrase_msg',       'phrase', 'phrase', $this->msg['phrase_msg'], 1);
		$sort->setSortItem('definition_msg',   'definition', 'definition', $this->msg['definition_msg']);
		$sort->setSortItem('display_once_msg', 'once', 'display_once', $this->msg['display_once_msg']);
		
		//$sort->getSql();
		//$sort->toHtml()
		return $sort;
	}
	
	
	function searchForm() {
		
		$tpl = &new tplTemplatez($this->template_dir . 'search_form.html');
		$tpl->tplAssign('hidden_fields', $this->getHiddenFields($_GET, array('phrase', 'do_search')));
		
		// search
		$search = new SearchParams();
		$search->setFormSubmitField(@$_GET['do_search']);
		
		//$search->setFullTextSearch();
		//$search->setParams(@$_GET['phrase'], 'phrase', array('phrase', 'definition'));
		
		$val = $search->setLikeSql(@$_GET['phrase']);
		$search->setParams($val, 'phrase', 'phrase');
		
		
		$tpl->tplAssign('search_msg', $this->msg['search_world_msg']);
		$tpl->tplParse($_GET);
		
		//$form_var =& $search->getVars();
		$val = array();
		$val['form'] = $tpl->tplPrint(1);
		$val['sql']  = $search->getSQL();
		
		return $val;
	}
	
	
	function parseLetterFilter(&$tpl, $vars) {
			
		//letter filter
		$get = $vars;
		unset($get['letter']);
		$str = http_build_query($get);
		
		foreach(range('a', 'z') as $k => $v) {
			$a['letter'] = strtoupper($v);
			$a['letter_link'] = $_SERVER['PHP_SELF'] . '?' . $str . '&letter=' . $v;
			$a['letter_class'] = (@$vars['letter'] == $v) ? 'tdSubTitle' : '';
			
			$tpl->tplParse($a, 'letter');
		}
	
	
		$ar = $vars; unset($ar['letter']);
		$tpl->tplAssign('all_letter_link', $_SERVER['PHP_SELF'] . '?'. http_build_query($ar));
		//$tpl->tplAssign('all_letter_msg', 'value');
	
		
		if(isset($vars['letter'])) {
			$sql = "phrase LIKE '". $vars['letter'] . "%'";
		} else {
			$sql = '1';
		}
		
		return $sql;
	}
		
}
?>
